Skip to content
c++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 100005;
constexpr int M = 1000006;
int mod;

struct edge {
    int u, v;
    bool operator<(const edge& o) const { return u == o.u ? v < o.v : u < o.u; }
    bool operator==(const edge& o) const { return u == o.u && v == o.v; }
} e[M];
int head[N], to[M], nxt[M], tot;
void add(int u, int v) {
    nxt[++tot] = head[u];
    head[u] = tot;
    to[tot] = v;
}
int dfn[N], low[N], timer;
int scc[N], scc_cnt;
int stk[N], top;

int in[N], cnt[N];
int scmx[N], ncmx[N];

int n, m;
void tarjan(int u) {
    dfn[u] = low[u] = ++timer;
    stk[++top] = u;
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (!dfn[v]) {
            tarjan(v);
            low[u] = std::min(low[u], low[v]);
        } else if (!scc[v]) {
            low[u] = std::min(low[u], dfn[v]);
        }
    }

    if (dfn[u] == low[u]) {
        ++scc_cnt;
        int v;
        int c = 0;
        do {
            c++;
            v = stk[top--];
            scc[v] = scc_cnt;
        } while (v != u);
        cnt[scc_cnt] = c;
    }
}
void rebuild(){
    int edge_cnt=0;
    for(int i=0;i<m;i++){
        int u=scc[e[i].u],v=scc[e[i].v];
        if(u==v)continue;
        e[edge_cnt++]={u,v};
    }
    std::sort(e,e+edge_cnt);
    int unique_cnt=std::unique(e,e+edge_cnt)-e;
    for(int i=1;i<=scc_cnt;i++)head[i]=0;tot=0;
    for(int i=0;i<unique_cnt;i++){
        add(e[i].u,e[i].v);
        in[e[i].v]++;
    }
} 
void fc() {
    std::cin >> n >> m >> mod;
    for (int i = 0; i < m; i++) {
        std::cin >> e[i].u >> e[i].v;
        add(e[i].u, e[i].v);
    }

    for (int i = 1; i <= n; i++)
        if (!dfn[i]) tarjan(i);

    rebuild();

    std::queue<int> q;
    for (int i = 1; i <= scc_cnt; i++)
        if (!in[i]) q.push(i), scmx[i] = cnt[i], ncmx[i] = 1;

    int mx = 0, nmx = 0;
    while (q.size()) {
        int u = q.front();
        q.pop();
        if (scmx[u] == mx) {
            nmx = (nmx + ncmx[u]) % mod;
        } else if (scmx[u] > mx) {
            mx = scmx[u];
            nmx = ncmx[u];
        }
        for (int i = head[u]; i; i = nxt[i]) {
            int v = to[i];
            int sum = scmx[u] + cnt[v];
            if (sum == scmx[v]) {
                ncmx[v] = (ncmx[v] + ncmx[u]) % mod;
            } else if (sum > scmx[v]) {
                scmx[v] = sum;
                ncmx[v] = ncmx[u];
            }
            if (--in[v] == 0) q.push(v);
        }
    }

    std::cout << mx << "\n" << nmx;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // std::cin>>t;
    while (t--) fc();
    return 0;
}